﻿#include "FITKAbaPredefinedFieldTemperature.h"
#include "FITKAbaStepManager.h"


Interface::FITKAbaAbstractPredefinedField::FITKAbaPredefinedFiledType Interface::FITKAbaPredefinedFieldTemperature::getPreDefinedType() const
{
    return FITKAbaPredefinedFiledType::Temperature;
}

bool Interface::FITKAbaPredefinedFieldTemperature::isModifiedInStep(int stepId) const
{
    if (m_modifiedDatas.contains(stepId))
        return true;
    return false;
}

void Interface::FITKAbaPredefinedFieldTemperature::activeStateChanged()
{

}

bool Interface::FITKAbaPredefinedFieldTemperature::isValidInStep(int stepId)
{
    return true;
}

bool Interface::FITKAbaPredefinedFieldTemperature::isValidInCreatedStep()
{
    return true;
}

Interface::FITKAbaPredefinedFieldTemperature::TemperatureDistribution Interface::FITKAbaPredefinedFieldTemperature::getDistribution() const
{
    return m_distribution;
}

void Interface::FITKAbaPredefinedFieldTemperature::setDistribution(TemperatureDistribution dis)
{
    m_distribution = dis;
}

double Interface::FITKAbaPredefinedFieldTemperature::getMagnitude(int stepId)
{
    return this->getDataInStep(stepId).getMagnitude();
}

void Interface::FITKAbaPredefinedFieldTemperature::setMagnitude(double mag, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setMagnitude(mag);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        this->getDataInStep(stepId).setMagnitude(mag);
    }
}

int Interface::FITKAbaPredefinedFieldTemperature::getAmplitudeId(int stepID)
{
    return this->getDataInStep(stepID).getAmplitudeId();
}

void Interface::FITKAbaPredefinedFieldTemperature::setAmplitudeId(int id, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setAmplitudeId(id);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        this->getDataInStep(stepId).setAmplitudeId(id);
    }
}


Interface::FITKAbaStepEnum::StateInStep Interface::FITKAbaPredefinedFieldTemperature::getStateInStep(int stepId)
{
    //数据创建所在的分析步索引
    int createdStepIndex = this->getStepIndex(m_createdStepId);
    if (createdStepIndex == -1)
        return FITKAbaStepEnum::StateInStep::None;
    //要判断状态的step的索引
    int stepIndex = this->getStepIndex(stepId);
    //manager中不存在该step，则返回无状态
    if (stepIndex == -1)
        return FITKAbaStepEnum::StateInStep::None;
    //如果要判断状态的step在创建所在的step之前，则返回无状态
    if (stepIndex < createdStepIndex)
        return FITKAbaStepEnum::StateInStep::None;
    if (stepId == m_createdStepId)
        return FITKAbaStepEnum::StateInStep::Created;
    if (!m_States.contains(stepId))
    {
        FITKAbaStepManager* stepMgr = this->getStepManager(this);
        if (stepIndex > 1)
        {
            FITKAbaAbstractStep* preStep = stepMgr->getDataByIndex(stepIndex - 1);
            FITKAbaStepEnum::StateInStep s = this->getStateInStep(preStep->getDataObjectID());
            if (s == FITKAbaStepEnum::StateInStep::ResetToInitial || s == FITKAbaStepEnum::StateInStep::NoLongerActive)
                return FITKAbaStepEnum::NoLongerActive;
            return FITKAbaStepEnum::StateInStep::Propagated;
        }
        return FITKAbaStepEnum::StateInStep::Propagated;
    }
    if (m_States[stepId] == FITKAbaStepEnum::StateInStep::Modified)
        if (this->isModifiedInStep(stepId))
            return FITKAbaStepEnum::StateInStep::Modified;
        else
            return FITKAbaStepEnum::StateInStep::Propagated;
    return m_States[stepId];
}

void Interface::FITKAbaPredefinedFieldTemperature::setStateInStep(FITKAbaStepEnum::StateInStep state, int stepId /*= 0*/)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        return;
    QList<FITKAbaStepEnum::StateInStep> ss{ FITKAbaStepEnum::StateInStep::Propagated,
        FITKAbaStepEnum::StateInStep::ResetToInitial, FITKAbaStepEnum::StateInStep::Modified };
    if (!ss.contains(state))
        return;
    m_States[stepId] = state;
    if (state == FITKAbaStepEnum::StateInStep::ResetToInitial)//清空后续分析步中的所有设置数据
    {
        FITKAbaStepManager* stepMgr = this->getStepManager(this);
        FITKAbaAbstractStep* step = stepMgr->getDataByID(stepId);
        int index = stepMgr->getDataIndex(step);
        int num = stepMgr->getDataCount();
        for (int i = index + 1; i < num; ++i)
        {
            FITKAbaAbstractStep* step = stepMgr->getDataByIndex(i);
            int id = step->getDataObjectID();
            if (m_States.contains(id))
                m_States.remove(id);
            if (m_modifiedDatas.contains(id))
                m_modifiedDatas.remove(id);
        }
    }
}

Interface::PredefinedTemperatureData& Interface::FITKAbaPredefinedFieldTemperature::getDataInStep(int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        return m_data;
    if (m_modifiedDatas.contains(stepId))
        return m_modifiedDatas[stepId];
    QVector<int> stepIds = this->getStepIds();
    int createdStepIndex = stepIds.indexOf(m_createdStepId);
    int stepIndex = stepIds.indexOf(stepId);
    if (!(createdStepIndex >= 0 && createdStepIndex < stepIds.size()))
        return m_data;
    if (!(stepIndex >= 0 && stepIndex < stepIds.size()))
        return m_data;
    for (int i = stepIndex; i >= createdStepIndex; --i)
    {
        int id = stepIds.at(i);
        if (m_modifiedDatas.contains(id))
            return m_modifiedDatas[id];
    }
    return m_data;
}

double Interface::PredefinedTemperatureData::getMagnitude() const
{
    return m_Magnitude;
}

void Interface::PredefinedTemperatureData::setMagnitude(double mag)
{
    m_Magnitude = mag;
}

int Interface::PredefinedTemperatureData::getAmplitudeId() const
{
    return m_amplitudeId;
}

void Interface::PredefinedTemperatureData::setAmplitudeId(int id)
{
    m_amplitudeId = id;
}



